﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

public static class HighScoreManager
{
    static string filename = "scores.dat";

	public static Dictionary<int, string> readScore(){

        String line;
        String[] lineValues;
        Dictionary<int, string> score = new Dictionary<int, string>();
    
        try
        {
            TextReader tr = new StreamReader(filename);
            while ((line = tr.ReadLine()) !=null)
            {
                lineValues = line.Split(';');
                score.Add(Int32.Parse(lineValues[0]), lineValues[1]);
            }

            score.OrderByDescending(k => k.Key);

            //http://stackoverflow.com/questions/289/how-do-you-sort-a-c-dictionary-by-value
            var sortedDict = (from entry in score orderby entry.Key descending select entry);
            score = sortedDict.ToDictionary<KeyValuePair<int, string>, int, string>(pair => pair.Key, pair => pair.Value);

            return score;

        }
        catch (Exception) { }
        
        return new Dictionary<int, string>();
    }

    public static void writeScore(int score)
    {
        TextWriter tw = new StreamWriter(filename, true);
        tw.WriteLine(score.ToString() + ";" + DateTime.Now.ToString());
        tw.Close();
    }

}
